package com.icss.servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Date;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.icss.beans.LogVO;
import com.icss.daos.LogDao;

public class LogServlet extends HttpServlet {

	/**
	 * 工作日志
	 */
	public LogServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {


	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
       
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
	    String log=request.getParameter("log");
		if(log!=null){
			if(log.equals("add")){
				String logType=request.getParameter("selectType");
				Date logDate=null;
				 if(request.getParameter("txtAddDate")!=null&&request.getParameter("txtAddDate")!=""){
					 logDate=Date.valueOf(request.getParameter("txtAddDate"));
	               } 
				String logTitle=request.getParameter("txtMobile");
				String logContent=request.getParameter("txtNote");
				String sql="insert into workLog(logType,logDate,logTitle,logContent) values('"+logType+"','"+logDate+"','"+logTitle+"','"+logContent+"')";
				LogDao dao=new LogDao();
				dao.insertWorkLog(sql);				
			}
			if(log.equals("query")){	
				List<LogVO> list=new ArrayList<LogVO>();
				Date beginDate=null;
				Date endDate=null;
				if(request.getParameter("txtBeginDate")!=null&&request.getParameter("txtBeginDate")!=""){
					beginDate=Date.valueOf(request.getParameter("txtBeginDate"));
	               } 
				if(request.getParameter("txtEndDate")!=null&&request.getParameter("txtEndDate")!=""){
					endDate=Date.valueOf(request.getParameter("txtEndDate"));
	               }
				String logType=request.getParameter("selectType");
				String logContent=request.getParameter("txtKey");
				String logTitle=request.getParameter("txtTitle");
				String sql=null;
				
				if(logType.equals("所有类型")){
					sql="select * from workLog where logType<>''";
				}else{sql="select * from workLog where logType='"+logType+"'";}
				if(beginDate!=null&&endDate!=null){
					sql = sql + " and logDate between '"+beginDate+"' and '"+endDate+"'";
				}else{
					if(beginDate!=null){
						sql = sql + " and logDate >=   '"+beginDate+"'";
					}
					if(endDate!=null){
						sql = sql + " and logDate <=   '"+endDate+"'";
					}
					
				}
				
				if(logTitle!=null&&logTitle!=""){
					sql = sql + " and logTitle like  '"+logTitle+"'";
				}
				if(logContent!=null&&logContent!=""){
					sql=sql+"and logContent like '"+logContent+"'";
				}
				System.out.println("SQL语句为："+sql);
				LogDao dao=new LogDao();
				list=dao.selectWorkLog(sql);
				if(list!=null){
					
					
					request.setAttribute("logVo", list);
	       			   String url="/page/office/log/edit_log.jsp";
	       			   RequestDispatcher rd=request.getRequestDispatcher(url);
	       			   rd.forward(request, response);   
	        	  }else{System.out.println("没有匹配的数据");}
				
				
				
			}
			
		}
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occure
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
